package com.ruoyi.web.controller.api;


import com.ruoyi.cat.domain.*;
import com.ruoyi.cat.domain.VO.ApiOracleVO;
import com.ruoyi.cat.service.*;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.ip.IpUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;

@Slf4j
@Api(tags = "api-广告接口")
@RestController
@RequestMapping("/api/ad")
public class AdController extends BaseController {


    @Autowired
    private ICatLogTableService catLogTableService;

    @Autowired
    private ICatActivityTableService iCatActivityTableService;

    @Autowired
    private ICatChanceService catChanceService;


    @Autowired
    private ICatShareuserTableService catShareuserTableService;



    @Autowired
    private IAdIprecordService adIprecordService;





    /**
     * 广告相关接口
     * @param apiOracleVO
     * @return
     */

    @PostMapping("/watch")
    @ApiOperation("看广告增加抽奖接口")
    public AjaxResult apiCodePayOrder(@RequestBody @Valid AdDataVO apiOracleVO) {
        log.info("广告相关接口====--------{}",apiOracleVO);
        apiOracleVO.setCreateTime(new Date());
        List<AdDataVO> list =  catLogTableService.selectCatAdData(apiOracleVO);
        if (list.size() >= 1) {
            return error("已观看过");
        }
        catLogTableService.upsertCatAdData(apiOracleVO);
        CatActivityTable catActivityTable = iCatActivityTableService.selectCatActivityTableByIdTask(apiOracleVO.getActivityId());
        if (catActivityTable == null) {
            return error("活动不存在");
        }
        Long invite = catActivityTable.getInviteChance();
        CatChance catCha = new CatChance();
        catCha.setUserId(apiOracleVO.getUserId());
        catCha.setArticleId(apiOracleVO.getActivityId());
        List<CatChance> catChances = catChanceService.selectCatChanceList(catCha);

        CatShareuserTable catShareuserTable = new CatShareuserTable();
        catShareuserTable.setArticleId(apiOracleVO.getActivityId());
        catShareuserTable.setUserId(apiOracleVO.getUserId());
        catShareuserTable.setType("2");
     //   CatShareuserTable catShareuserTable1 = catShareuserTableService.getShareUser(catShareuserTable);
        log.info("登录相关的内容====--------{}",catChances);
        if (!catChances.isEmpty()) {
            CatChance ce = new CatChance();
            ce.setRemainingChances(invite);
            ce.setUserId(apiOracleVO.getUserId());
            ce.setArticleId(apiOracleVO.getActivityId());
          //  ce.setReferenceId(loginVO.getReferenceId());
            log.info("ce登录相关的内容====--------{}",ce);
            Integer i = catChanceService.updateRemainingChances(ce);
            catShareuserTableService.insertCatShareuserTable(catShareuserTable);
            return success(i);
        }
        return success();
    }

    @PostMapping("/watchH5")
    @ApiOperation("看广告增加抽奖接口")
    public AjaxResult apiCodePayOrderH5(HttpServletRequest request, @RequestBody @Valid AdDataVO apiOracleVO) {
        log.info("广告相关接口====--------{}",apiOracleVO);
        String ipAddress = IpUtils.getIpAddr(request);
        AdIprecord adIprecord = new AdIprecord();
        adIprecord.setCreateTime(new Date());
        adIprecord.setIpValue(ipAddress);
        adIprecord.setArticleId(apiOracleVO.getActivityId());
        List<AdIprecord>  list =  adIprecordService.selectAdIprecordList(adIprecord);
        if (list.size() >= 1) {
            return error("已观看过");
        }
        return success();
    }


}
